package com.cyl.manager.pms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sincere.common.annotation.Log;
import com.sincere.common.core.controller.BaseController;
import com.sincere.common.enums.BusinessType;
import com.cyl.manager.pms.service.SkuService;
import com.cyl.manager.pms.req.SkuReq;
import com.cyl.manager.pms.req.SkuQueryReq;
import com.cyl.manager.pms.resp.SkuResp;
import com.sincere.common.utils.poi.ExcelUtil;
import java.util.List;

/**
 * sku信息Controller
 * 
 * @author zcc
 * @date 2022-11-28
 */
@Api(description ="SKU管理接口列表")
@RestController
@RequestMapping("/pms/sku")
@RequiredArgsConstructor
public class SkuController extends BaseController {

    private final SkuService skuService;

    @ApiOperation("查询SKU列表")
    @PreAuthorize("@ss.hasPermi('pms:sku:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<SkuResp>> list(@RequestBody SkuQueryReq query, Pageable page) {
        return ResponseEntity.ok(skuService.querySku(query, page));
    }

    @ApiOperation("导出SKU列表")
    @PreAuthorize("@ss.hasPermi('pms:sku:export')")
    @Log(title = "SKU", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(SkuQueryReq query) {
        List<SkuResp> list = skuService.exportSkuList(query);
        ExcelUtil<SkuResp> util = new ExcelUtil<>(SkuResp.class);
        return ResponseEntity.ok(util.writeExcel(list, "SKU数据"));
    }

    @ApiOperation("获取SKU详细信息")
    @PreAuthorize("@ss.hasPermi('pms:sku:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<SkuResp> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(skuService.getSkuDetail(id));
    }

    @ApiOperation("新增SKU")
    @PreAuthorize("@ss.hasPermi('pms:sku:add')")
    @Log(title = "SKU", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Boolean> create(@RequestBody SkuReq req) {
        return ResponseEntity.ok(skuService.save(req));
    }

    @ApiOperation("修改SKU")
    @PreAuthorize("@ss.hasPermi('pms:sku:edit')")
    @Log(title = "SKU", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Boolean> update(@RequestBody SkuReq req) {
        return ResponseEntity.ok(skuService.update(req));
    }

    @ApiOperation("删除SKU")
    @PreAuthorize("@ss.hasPermi('pms:sku:remove')")
    @Log(title = "SKU", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public ResponseEntity<Boolean> remove(@PathVariable Long id) {
        return ResponseEntity.ok(skuService.deleteById(id));
    }
}
